package com.btc.study.sort;

import java.util.Arrays;

/**
 * @Auther: hq
 * @Description: 冒泡排序
 * @Date 2019/6/24 10:40
 */
public class BubbleSort {

    public static int[] bubbleSort(int[] a, int n){
        if(n <= 1){
            return a;
        }
        for(int i = 0 ; i < n - 1; i ++){
            boolean flag = false;
            for(int j = 0 ; j < n - i - 1; j ++ ){
                if(a[j] > a[j + 1]){
                    int tmp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1 ] = tmp;
                    flag = true;
                }
            }
            System.out.println(Arrays.toString(a));
            if(!flag){
                break;
            }
        }
        return a;
    }

    public static void main(String[] args){
        int a[] = {4,2,3,1,5,1};
        System.out.println("============ bubbleSort ============");
        bubbleSort(a, 6);
    }


}
